package com.whut.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.whut.mapper.ResultsinglechildMapper;
import com.whut.model.Resultsinglechild;
import com.whut.service.FileService;
import com.whut.utils.FileUtil;
import com.whut.utils.FormatDate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
*   文件上传
* @author fuzhenjie
* @date 19.8.2 16:17
*/
@Service("FileService")
public class FileServiceImpl implements FileService {

    @Resource
    private ResultsinglechildMapper resultsinglechildMapper;

    @Override
    public String addResultSingleChild(String params, List<MultipartFile> files) {
        Resultsinglechild resultsinglechild = JSON.toJavaObject(JSONObject.parseObject(params), Resultsinglechild.class);
        System.out.println(resultsinglechild);

        System.out.println(files.size());
        String absolutePath = "";
        JSONArray imgs = new JSONArray();
        try {
            for(int i = 0;i<files.size();i++){
                String fileName = FormatDate.getDateStringYMDHms1()+FormatDate.getFormatNumber000(i) +".png";
                absolutePath = FileUtil.upload(files.get(i).getInputStream(),"img/", fileName);
                imgs.add(fileName);
            }
        }catch (IOException e){
            e.printStackTrace();
            return "error";
        }
        resultsinglechild.setPicPath(absolutePath);
        resultsinglechild.setPicFileName(imgs.toJSONString());


        resultsinglechild.setCheckTime(FormatDate.getDateStringYMDHms());
        resultsinglechild.setIsSubmit("N");
        try{
            resultsinglechildMapper.insert(resultsinglechild);
            return "succ";
        }catch(Exception e){
            e.printStackTrace();
            return "error";
        }

    }
}
